﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Reflection;

namespace WAF.AppServer
{
    static class Program
    {

        /// <summary>
        /// Application Server Entry Point
        /// </summary>
        static void Main()
        {
            bool runOnConsole = false;
            foreach (string arg in Environment.GetCommandLineArgs())
            {
                if (arg.ToLower() == "-console")
                    runOnConsole = true;
            }            

            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
			{ 
				new AppService() 
			};
            if (runOnConsole)
            {
                Console.WriteLine("Starting app server services"); 
                foreach (ServiceBase service in ServicesToRun)
                {
   
                    MethodInfo onStart = typeof(ServiceBase).GetMethod("OnStart", BindingFlags.NonPublic | BindingFlags.Instance);
                    string[] startArgs = new string[0];
                    onStart.Invoke(service, new object[] { startArgs });
                }
                Console.WriteLine("App server services are started, press any key to stop");
                Console.ReadKey();
                Console.WriteLine("Stopping app server services");
                foreach (ServiceBase service in ServicesToRun)
                {

                    MethodInfo onStop = typeof(ServiceBase).GetMethod("OnStop", BindingFlags.NonPublic | BindingFlags.Instance);
                    onStop.Invoke(service, new object[] { });
                }
                Console.WriteLine("App server services are stopped, press any key to end");
                Console.ReadKey();
            }
            else
                ServiceBase.Run(ServicesToRun);
        }
    }
}
